package com.ys.peony.model;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.ys.peony.app.HttpURL;
import com.ys.peony.base.BaseModel;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.model.domain.CarType;
import com.ys.peony.model.domain.CityModel;
import com.ys.peony.model.domain.SaveCarModel;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 59153 on 2017/7/31.
 */

public class CarPledgeModel extends BaseModel implements CarPledgeContract.iCarPledgeModel {
    private static final String TAG = "CarPledgeModel";
    private static final String PARAM_LOADID = "loanId";
    private static final String PARAM_USERID = "userId";

    private static final String PARAM_VIN = "vin";
    private static final String PARAM_TOKEN = "token";

    private Handler mHandler = new Handler(Looper.getMainLooper());

    private CarPledgeContract.iCarPledgePresenter carPledgePresenter;

    public CarPledgeModel(CarPledgeContract.iCarPledgePresenter iCarPledgePresenter) {
        carPledgePresenter = iCarPledgePresenter;
    }

    @Override
    public void getCarByVin(String vin) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(PARAM_VIN, vin);
        httpManagerUtils.get(HttpURL.GET_CARINFO_VIN, params, new BaseRespCallBack() {
            public void onRespSuccess(String respStr) {
                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                if (!TextUtils.isEmpty(respStr)) {
                    String status = "";
                    String msg = "";
                    try {
                        org.json.JSONObject jsonObject = new org.json.JSONObject(respStr);
                        if(jsonObject != null) {
                            status = jsonObject.getString("status");
                        }
                        if ("1".equals(status)) {
                            com.ys.peony.model.domain.CarModel carModel = JSONUtil.parseObject(respStr, com.ys.peony.model.domain.CarModel.class);
                            carPledgePresenter.onSuccess(carModel);
                        } else if ("0".equals(status)){
                            msg = jsonObject.getString("error_msg");
                            onRespFailure(msg);
                        }
                    } catch (org.json.JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                LogUtil.i(TAG, "onRespFailure:" + msg);
                carPledgePresenter.onFail(msg);
            }

        });
    }

    @Override
    public void getCarInfoById(long loanId, long userId) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(PARAM_LOADID, loanId);
        params.put(PARAM_USERID, userId);
        httpManagerUtils.get(HttpURL.MORTAGESEE, params, new BaseRespCallBack(BaseRespCallBack.TAG_NO_DISMISS_DIALOG) {
            public void onRespSuccess(final String respStr) {
                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                final CarDetailModel carDetailModel = JSONUtil.parseObject(respStr, CarDetailModel.class);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        carPledgePresenter.onSuccess(carDetailModel);
                    }
                });

            }

            @Override
            public void onRespFailure(final String msg) {
                super.onRespFailure(msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespFailure:" + msg);
                        carPledgePresenter.onFail(msg);
                    }
                });

            }

        });
    }

    @Override
    public void getCity() {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.GET_CITYS, null, new BaseRespCallBack() {
            public void onRespSuccess(String respStr) {
                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                CityModel cityModel = JSONUtil.parseObject(respStr, CityModel.class);
                carPledgePresenter.onSuccess(cityModel);
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                LogUtil.i(TAG, "onRespFailure:" + msg);
                carPledgePresenter.onFail(msg);
            }

        });
    }

    @Override
    public void addPledgeCar(Map<String, Object> params) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.mortgageAdd, params, new BaseRespCallBack() {
            public void onRespSuccess(String respStr) {
                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                if (!TextUtils.isEmpty(respStr)) {
                    SaveCarModel saveCarModel = JSONUtil.parseObject(respStr, SaveCarModel.class);
                    carPledgePresenter.onSuccess(saveCarModel);
                }
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                LogUtil.i(TAG, "onRespFailure:" + msg);
                carPledgePresenter.onFail(msg);
            }

        });
    }

    @Override
    public void updatePledgeCar(Map<String, Object> params) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.mortgageUpdate, params, new BaseRespCallBack(BaseRespCallBack.TAG_NO_DISMISS_DIALOG) {
            public void onRespSuccess(final String respStr) {

                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                if (!TextUtils.isEmpty(respStr)) {
                    final SaveCarModel saveCarModel = JSONUtil.parseObject(respStr, SaveCarModel.class);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            carPledgePresenter.onSuccess(saveCarModel);
                        }
                    });
                }


            }

            @Override
            public void onRespFailure(final String msg) {
                super.onRespFailure(msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespFailure:" + msg);
                        carPledgePresenter.onFail(msg);
                    }
                });

            }

        });
    }

    @Override
    public void addBaoDanInfo(Map<String, Object> params) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.BAODAN_ADD, params, new BaseRespCallBack(BaseRespCallBack.TAG_NO_DISMISS_DIALOG) {
            public void onRespSuccess(final String respStr) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespSuccess:" + respStr);
                        if (!TextUtils.isEmpty(respStr)) {
                            carPledgePresenter.onSuccess(CarPledgeContract.ADD_CAR_BAODAN);
                        }
                    }
                });

            }

            @Override
            public void onRespFailure(final String msg) {
                super.onRespFailure(msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespFailure:" + msg);
                        carPledgePresenter.onFail(msg);
                    }
                });

            }

        });
    }

    @Override
    public void deleteBaoDan(Map<String, Object> params) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.BAODAN_DELETE, params, new BaseRespCallBack(BaseRespCallBack.TAG_NO_DISMISS_DIALOG) {
            public void onRespSuccess(final String respStr) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespSuccess:" + respStr);
                        if (!TextUtils.isEmpty(respStr)) {
                            carPledgePresenter.onSuccess(CarPledgeContract.DELETE_CAR_BAODAN);
                        }
                    }
                });

            }

            @Override
            public void onRespFailure(final String msg) {
                super.onRespFailure(msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.i(TAG, "onRespFailure:" + msg);
                        carPledgePresenter.onFail(msg);
                    }
                });

            }

        });
    }

    @Override
    public void deleteCar(Map<String, Object> params) {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        httpManagerUtils.get(HttpURL.DELETE_CAR, params, new BaseRespCallBack() {
            public void onRespSuccess(String respStr) {
                LogUtil.i(TAG, "onRespSuccess:" + respStr);
                if (!TextUtils.isEmpty(respStr)) {
                    carPledgePresenter.onSuccess(CarPledgeContract.DELETE_CAR);
                }
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                LogUtil.i(TAG, "onRespFailure:" + msg);
                carPledgePresenter.onFail(msg);
            }

        });
    }

}
